#include "resources.h"
#include "system_tab.h"
#include "globals.h"
#include "main_dialog.h"
#include "utility.h"
#include "dxdiagcom.h"
#include <stdio.h>
#include <time.h>

/* System Tab window logic */
INT_PTR CALLBACK SystemTabDialogProc(HWND hwnd, UINT message,
                                     WPARAM wParam, LPARAM lParam)
{
    DxDiag_SystemInfo *systemInfo = g_dxdiagcom_root.systemInfo;
    switch (message) {
    case WM_INITDIALOG:
        SetDlgItemTextW(hwnd, DLG_SYS_DATETIME_DATA,
                        systemInfo->szTimeLocalized);
        SetDlgItemTextW(hwnd, DLG_SYS_CMPNAME_DATA,
                        systemInfo->szMachineNameLocalized);
        SetDlgItemTextW(hwnd, DLG_SYS_OSNAME_DATA,
                        systemInfo->szOSExLocalized);
        SetDlgItemTextW(hwnd, DLG_SYS_LANG_DATA,
                        systemInfo->szLanguagesLocalized);
        SetDlgItemTextW(hwnd, DLG_SYS_SYSMAN_DATA,
                        systemInfo->szSystemManufacturerEnglish);
        SetDlgItemTextW(hwnd, DLG_SYS_MODEL_DATA,
                        systemInfo->szSystemModelEnglish);
        SetDlgItemTextW(hwnd, DLG_SYS_BIOS_DATA,
                        systemInfo->szBIOSEnglish);
        SetDlgItemTextW(hwnd, DLG_SYS_CPU_DATA,
                        systemInfo->szProcessorEnglish);
        SetDlgItemTextW(hwnd, DLG_SYS_PAGEFILE_DATA,
                        systemInfo->szPageFileLocalized);
        SetDlgItemTextW(hwnd, DLG_SYS_MEM_DATA,
                        systemInfo->szPhysicalMemoryEnglish);
        SetDlgItemTextW(hwnd, DLG_SYS_DXVER_DATA,
                        systemInfo->szDirectXVersionLongEnglish);
        return TRUE;
    default:
        return FALSE;
    }
}

/* Populates the label fields on the System Tab */
VOID PopulateSystemTabLabels(HWND hSystemTabDialog)
{
    /* Groupbox */
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_GRP_INFO, DLG_SYS_INFO);

    /* Labels */
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_DATETIME, DLG_SYS_DATETIME);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_CMPNAME, DLG_SYS_CMPNAME);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_OSNAME, DLG_SYS_OSNAME);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_LANG, DLG_SYS_LANG);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_SYSMAN, DLG_SYS_SYSMAN);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_MODEL, DLG_SYS_MODEL);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_BIOS, DLG_SYS_BIOS);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_CPU, DLG_SYS_CPU);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_PAGEFILE, DLG_SYS_PAGEFILE);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_DXVER, DLG_SYS_DXVER);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_MEM, DLG_SYS_MEM);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_BANNER, DLG_SYS_BANNER);
    set_control_text_resource(hSystemTabDialog,
                              STRING_DLG_SYS_WHQL_CHK_LBL,
                              DLG_SYS_WHQL_CHK);
    set_control_text_resource(hSystemTabDialog, STRING_DLG_SYS_DXDIAG_VER,
                              DLG_SYS_DXDIAG_VER);

    /* Check WHQL */
    SendMessageW(GetDlgItem(hSystemTabDialog, DLG_SYS_WHQL_CHK),
                 BM_SETCHECK, BST_CHECKED, 0);
}

/* Handles System Tab resizing */
VOID ResizeSystemTab(HWND hSystemTab, RECT tabDisplayArea)
{
    HANDLE handle;
    RECT dlus;
    LONG tabWidth, tabHeight;
    LONG sysGrpLeft, sysGrpWidth, sysGrpTop, sysGrpHeight;
    LONG sysBannerLeft, sysBannerWidth, sysBannerTop, sysBannerHeight;
    LONG sysDateLeft, sysDateWidth, sysDateTop, sysDateHeight;

    /* Tab width/height */
    tabWidth = tabDisplayArea.right - tabDisplayArea.left;
    tabHeight = tabDisplayArea.bottom - tabDisplayArea.top;

    /* Get dialog units */
    dlus.left = 0;
    dlus.right = DLG_UNIT_DEF_WIDTH;
    dlus.top = 0;
    dlus.bottom = DLG_UNIT_DEF_HEIGHT;
    MapDialogRect(hSystemTab, &dlus);

    /* Set dialog size */
    SetWindowPos(hSystemTab, NULL, tabDisplayArea.left, tabDisplayArea.top,
                 tabWidth, tabHeight, 0);

    /* System Tab Banner */
    handle = GetDlgItem(hSystemTab, DLG_SYS_BANNER);
    sysBannerLeft = WND_DEF_SPACING;
    sysBannerTop = WND_DEF_SPACING;
    sysBannerWidth = tabWidth - WND_DEF_SPACING * 2;
    sysBannerHeight = dlus.bottom * 7;
    SetWindowPos(handle, NULL, sysBannerLeft, sysBannerTop, sysBannerWidth,
                 sysBannerHeight, 0);

    /* System Info groupbox */
    handle = GetDlgItem(hSystemTab, DLG_SYS_INFO);
    sysGrpLeft = sysBannerLeft;
    sysGrpTop = sysBannerTop + sysBannerHeight + WND_DEF_SPACING;
    sysGrpWidth = sysBannerWidth;
    sysGrpHeight = tabHeight - dlus.bottom * 1.5 - sysGrpTop;
    SetWindowPos(handle, NULL, sysGrpLeft, sysGrpTop, sysGrpWidth,
                 sysGrpHeight, 0);

    /* Labels */
    handle = GetDlgItem(hSystemTab, DLG_SYS_DATETIME);
    sysDateLeft = WND_DEF_SPACING;
    sysDateTop = sysGrpTop + dlus.bottom * 2;
    sysDateWidth = tabWidth * 1 / 3;
    sysDateHeight = dlus.bottom;
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_DATETIME_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    sysDateTop += dlus.bottom * 1.5;
    handle = GetDlgItem(hSystemTab, DLG_SYS_CMPNAME);
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_CMPNAME_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    sysDateTop += dlus.bottom * 1.5;
    handle = GetDlgItem(hSystemTab, DLG_SYS_OSNAME);
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_OSNAME_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    sysDateTop += dlus.bottom * 1.5;
    handle = GetDlgItem(hSystemTab, DLG_SYS_LANG);
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_LANG_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    sysDateTop += dlus.bottom * 1.5;
    handle = GetDlgItem(hSystemTab, DLG_SYS_SYSMAN);
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_SYSMAN_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    sysDateTop += dlus.bottom * 1.5;
    handle = GetDlgItem(hSystemTab, DLG_SYS_MODEL);
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_MODEL_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    sysDateTop += dlus.bottom * 1.5;
    handle = GetDlgItem(hSystemTab, DLG_SYS_BIOS);
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_BIOS_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    sysDateTop += dlus.bottom * 1.5;
    handle = GetDlgItem(hSystemTab, DLG_SYS_CPU);
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_CPU_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    sysDateTop += dlus.bottom * 1.5;
    handle = GetDlgItem(hSystemTab, DLG_SYS_MEM);
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_MEM_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    sysDateTop += dlus.bottom * 1.5;
    handle = GetDlgItem(hSystemTab, DLG_SYS_PAGEFILE);
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_PAGEFILE_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    sysDateTop += dlus.bottom * 1.5;
    handle = GetDlgItem(hSystemTab, DLG_SYS_DXVER);
    SetWindowPos(handle, NULL, sysDateLeft, sysDateTop, sysDateWidth,
                 sysDateHeight, 0);
    handle = GetDlgItem(hSystemTab, DLG_SYS_DXVER_DATA);
    SetWindowPos(handle, NULL, sysDateLeft + sysDateWidth + dlus.right,
                 sysDateTop, sysDateWidth, sysDateHeight, 0);

    handle = GetDlgItem(hSystemTab, DLG_SYS_WHQL_CHK);
    SetWindowPos(handle, NULL, sysGrpLeft + WND_DEF_SPACING * 2,
                 sysGrpTop + sysGrpHeight - dlus.bottom -
                 WND_DEF_SPACING * 2, sysGrpWidth, sysDateHeight, 0);

    handle = GetDlgItem(hSystemTab, DLG_SYS_DXDIAG_VER);
    SetWindowPos(handle, NULL, sysDateLeft, tabHeight - dlus.bottom * 1.5,
                 sysBannerWidth, dlus.bottom, 0);

}

VOID createSystemTabDialog(Tab * tab)
{
    HWND hSystemTabDialog;

    /* Create system dialog */
    hSystemTabDialog
        = CreateDialogW(g_hinstance, MAKEINTRESOURCEW(DLG_SYSTEM),
                        g_hmaindialog, SystemTabDialogProc);
    tab_initialize(tab, hSystemTabDialog, ResizeSystemTab);
    if (hSystemTabDialog == NULL) {
        messageboxw_resource(NULL, STRING_COMMON_WINDOW_CREATION_ERROR,
                             STRING_COMMON_ERROR, MB_OK | MB_ICONERROR);
    }

    PopulateSystemTabLabels(hSystemTabDialog);
}
